package com.naizhuai.util.command;


import android.annotation.SuppressLint;
import android.app.IntentService;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.support.annotation.Nullable;

import com.naizhuai.Model.ModelProvider;
import com.naizhuai.Model.db.eneity.ManageNoteBean;
import com.naizhuai.Model.db.managenote.ManageNoteRepository;
import com.naizhuai.Model.db.managenote.ManageNoteSourse;
import com.naizhuai.network.exception.ApiException;
import com.naizhuai.ui.management.presenter.CompanyManageDataApiStoreIml;
import com.naizhuai.util.AppUtils;
import com.naizhuai.util.ConstantsParams;
import com.naizhuai.util.ConstantsState;
import com.naizhuai.util.NaizhuAILog;
import com.naizhuai.util.UidUtil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import rx.Observer;


public class CommandsService extends Service {

    private ManageNoteRepository mManageNoteRepository;

    private Handler handler = new Handler();

    public CommandsService() {
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    private Context getContext() {
        return this;
    }

    @Override
    public void onCreate() {
        initData();
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        initData();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                requestNetData();
            }
        }, ConstantsParams.getAskServerTriggerTimer());
        return super.onStartCommand(intent, flags, startId);
    }


    private void requestNetData() {
        mManageNoteRepository.getNoteDataAsyn(ConstantsState.STATE_NO_SEE, new ManageNoteSourse.GetManageNoteCallback() {
            @Override
            public void onGetStateData(List<ManageNoteBean> search) {
                if (AppUtils.isServiceRunning(getContext(), CommandsService.class.getName())) {
                    NaizhuAILog.e("CommandsService.class.getName()>>>>>>is running");
                }
                if (search.size() > 0) {
                    Intent intent = new Intent();
                    ArrayList<String> applyIds = new ArrayList<>();
                    for (ManageNoteBean bean : search)
                        applyIds.add(bean.getApplyId());
                    CompanyManageDataApiStoreIml.getInstance().getApplyStatus(new UidUtil(applyIds, "applyId").toString()).subscribe(new Observer<List<ManageNoteBean>>() {
                        @Override
                        public void onCompleted() {

                        }

                        @Override
                        public void onError(Throwable e) {
                            ApiException.showException(e, getContext());
                            RemindReceiver.remindReceiver(getContext(), intent);
                            stopSelf();
                        }

                        @Override
                        public void onNext(List<ManageNoteBean> manageNoteBeans) {
                            mManageNoteRepository.saveNoteData(manageNoteBeans);
                            if (hasChangeData(manageNoteBeans)) {
                                RemindReceiver.updateReceiver(getContext(), intent);
                            } else
                                RemindReceiver.remindReceiver(getContext(), intent);
                            stopSelf();
                        }
                    });
                }
            }

            @Override
            public void onDataNotAvailable() {

            }
        });
    }

    private boolean hasChangeData(List<ManageNoteBean> manageNoteBeans) {
        for (ManageNoteBean bean : manageNoteBeans) {
            if (bean.getStatus() == ConstantsState.STATE_AGREE)
                return true;
        }
        return false;
    }

    private void initData() {
        if (mManageNoteRepository == null)
            mManageNoteRepository = ModelProvider.provideManageNoteRepository();
    }

    public static void startInitService(Context mContext, Intent intent) {
        if (AppUtils.isServiceRunning(mContext, CommandsService.class.getName())) {
            NaizhuAILog.e("CommandsService.class.getName()>>>>>>is running");
        }
        if (!AppUtils.isServiceRunning(mContext, CommandsService.class.getName())) {
            if (intent == null) {
                intent = new Intent(mContext, CommandsService.class);
            }
            mContext.startService(intent);
        }
    }

}

